<?php
ini_set('error_reporting', E_ERROR);
include_once "inc_function.php";
?>
<body>
<script type="text/javascript" src="<?= 'http://'.JS_PATH;?>form/js_form.js"></script>
<script type="text/javascript">
	function lookup_data(id,c_name){
    	$("#txt_country_name").val(c_name);
    	$("#country_id").val(id);
	}
</script>
<?php
//   $curs_job = oci_new_cursor($con);
//   $stmt = oci_parse($con,"begin pkg_master.sp_get_job_list(1,:data);end;");
//   oci_bind_by_name($stmt,":data",&$curs_job,-1,OCI_B_CURSOR);
//   oci_execute($stmt,OCI_DEFAULT);
//   oci_execute($curs_job,OCI_DEFAULT);
//   oci_free_statement($stmt);
//
//   $curs_company = oci_new_cursor($con);
//   $stmt = oci_parse($con,"begin pkg_master.sp_get_company_list(1,:data);end;");
//   oci_bind_by_name($stmt,":data",&$curs_company,-1,OCI_B_CURSOR);
//   oci_execute($stmt,OCI_DEFAULT);
//   oci_execute($curs_company,OCI_DEFAULT);
//   oci_free_statement($stmt);
//
//   $curs_division = oci_new_cursor($con);
//   $stmt = oci_parse($con,"begin pkg_master.sp_get_division_list(1,:data);end;");
//   oci_bind_by_name($stmt,":data",&$curs_division,-1,OCI_B_CURSOR);
//   oci_execute($stmt,OCI_DEFAULT);
//   oci_execute($curs_division,OCI_DEFAULT);
//   oci_free_statement($stmt);

//$status =  ($_POST['emp_active'] == 'on' && isset($_POST['emp_active'])) ? 1 : 0;

   switch($_POST['action']){
      case 'add' :
         $stmt = oci_parse($con,"begin pkg_master.sp_insert_m_city(:s_city_name,:s_city_code,:s_update_id,:s_create_id,:s_country_id);end;");
         oci_bind_by_name($stmt,":s_city_name",$_POST['txt_city_name']);
         oci_bind_by_name($stmt,":s_city_code",$_POST['txt_city_code']);
         oci_bind_by_name($stmt,":s_update_id",$_POST['person']);         
         oci_bind_by_name($stmt,":s_create_id",$_POST['person']);
         oci_bind_by_name($stmt,":s_country_id",$_POST['country_id']);
         oci_execute($stmt,OCI_DEFAULT);

         	$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{	
				echo "<script>alert(connection failure, please try again!!!)</script>";				
		    	$e = oci_error($con);
				oci_rollback($con);
			}								
         
		 header("Location: list_city.php");
		//var_dump($_POST);
      	break;
      case 'update' :      	      
         $stmt = oci_parse($con,"begin pkg_master.sp_update_m_city(:s_city_id,:s_city_code,:s_city_name,:s_country_code,:s_update_id);end;");
         oci_bind_by_name($stmt,":s_city_id",$_REQUEST['update']);
         oci_bind_by_name($stmt,":s_city_code",$_POST['txt_city_code']);
         oci_bind_by_name($stmt,":s_city_name",$_POST['txt_city_name']);
         oci_bind_by_name($stmt,":s_country_code",$_POST['country_id']);
         oci_bind_by_name($stmt,":s_update_id",$_POST['person']);
         oci_execute($stmt,OCI_DEFAULT);
			
         	$r = oci_commit($con);
         	         
			// Commit the changes to both tables
			if (!$r) 
			{					
				echo "<script>alert(connection failure, please try again!!!)</script>";				
				echo $e = oci_error($con);
				oci_rollback($con);
			}								
         
     		header("Location: list_city.php");
      break;
      default :
      break;
   }

   if($_REQUEST['form'] == 'update' && isset($_REQUEST['form'])){
		    $curs = oci_new_cursor($con);
		   	$stmt = OCIParse($con,"begin pkg_master.sp_get_city_by_id(:city_id,:c_city_list); end;");
		   	OCIBindByName($stmt,":city_id",$_REQUEST['update']);
		   	OCIBindByName($stmt,":c_city_list",$curs,-1,OCI_B_CURSOR);
		   	ociexecute($stmt,OCI_DEFAULT);
		   	ociexecute($curs,OCI_DEFAULT);

   	while (OCIFetchInto($curs,$o_city_list))
  	   {
       	$city_id   = $o_city_list[0];
       	$city_name = $o_city_list[1];
       	$city_code = $o_city_list[2];
       	$country_code = $o_city_list[7];
       	$country_name = $o_city_list[9];
  	   }
  	   oci_free_statement($stmt);
  	   oci_free_statement($curs);

      $checked = ($status) ? 'checked=checked' : '';
   }

   $form = ($_REQUEST['form'] == 'update' && isset($_REQUEST['form'])) ? 'update' : 'add' ;

   function chekedValidation($content,$validation){
      if($content == $validation){
         return 'selected=selected';
      }
   }
?>
<form name='frm_emp_register' id="frm_emp_register" action method='post' onsubmit='return submitValidation(this.id)'>
<fieldset class="frm_registered">
   <legend> Form <?= ucwords($form);?> City</legend>
   <input type='hidden' name='country_id' id='country_id' value="<?= $country_code ?>" />
   <input type='hidden' name='action' value="<?= $form ?>" />
<!--   <input type='hidden' name='person' id='person' value="<?= $_SESSION['employee_id']?>" />-->
   <input type='hidden' name='person' id='person' value="290" />
   
   <div class='formInput'>
   
   <div>
   	<label> City Code <span class='red'>*</span></label> : <input type='text' name='txt_city_code' class='textbox' id='txt_city_code' value='<?= $city_code;?>' maxlength=3 onBlur="return newCekValidation('txt',this.id);"/>
      <sup class='red error_txt_city_code hidden'> * Required </sup>
<!--   	<input type='checkbox' name='city_active' id='city_active' style='width:30px;' <?= $checked;?>/>Active-->
   </div>
   <div>
   	<label> City Name <span class='red'>*</span></label> : <input type='text' name='txt_city_name' class='textbox' id='txt_city_name' value='<?= $city_name; ?>' maxlength=20 onBlur="return newCekValidation('txt',this.id);"/>      
      <sup class='red error_txt_city_name hidden'> * Required </sup>
   </div>
   
   <div>
   	<label> Country Name <span class='red'>*</span></label> : <input type='text' name='txt_country_name' class='textbox' id='txt_country_name' value='<?= $country_name ?>' readonly onBlur="return newCekValidation('txt',this.id);"/>
      <a href='<?= AJAX_PATH?>list_country.php?auto=true&keepThis=true&TB_iframe=true&height=400&width=500' class='thickbox'><img src='http://<?= IMAGE_PATH?>search.png' style='cursor:pointer;'/></a>
      <sup class='red error_txt_country_name hidden'> * Required </sup>
   </div>
   
   </div>
   <div class="mybutton"> <button type="submit" class="button"> Save </button> <button type="reset" class="button"> Reset </button></div>

</fieldset>
</form>
</body>
<?php
//   oci_free_statement($curs_job);
//   oci_free_statement($curs_company);
//   oci_free_statement($curs_division);
?>